<template>
  <div class="main">
    <!-- 第一个容器 -->
    <div class="container">
      <div class="rectangle"></div>
      <div class="content">
        <div class="text">
          <span class="bold animated-text">{{ animatedText1 }}</span>
          <span class="normal-text">成为高贵的VIP用户，你将拥有十分亮眼的标志，尽现无上豪华！</span>
        </div>
        <div class="image">
          <img v-if="position === 'right'" src="https://pic.imgdb.cn/item/649904991ddac507cc90faf5.png"
            alt="Left Image">
        </div>
      </div>
      <div class="underline"></div>
    </div>

    <!-- 第二个容器 -->
    <div class="container pink-container">
      <div class="image">
        <img v-if="position === 'right'" src="https://pic.imgdb.cn/item/64993ecc1ddac507ccec1982.png" alt="Left Image">
      </div>
      <div class="content">
        <div class="text">
          <span class="bold animated-text">{{ animatedText2 }}</span>
          <span class="normal-text">成为尊贵的VIP用户后，你将拥有每次订车享有高额折扣，订的越多折扣越多！</span>
        </div>
        <div class="pink-rectangle"></div>
      </div>
      <div class="pink-underline"></div>
    </div>

    <!-- 第三个容器 -->
    <div class="container">
      <div class="rectangle"></div>
      <div class="content">
        <div class="text">
          <span class="bold animated-text">{{ animatedText3 }}</span>
          <span class="normal-text">心动了吗，那就点击下方按钮成为尊贵的VIP用户！</span>
        </div>
        <div class="image">
          <img v-if="position === 'right'" src="https://pic.imgdb.cn/item/649945f71ddac507ccf9d8ff.png"
            alt="Left Image">
        </div>
      </div>
      <div class="underline"></div>
    </div>

    <!-- 新增的盒子 -->
    <div class="button-container">
      <button class="green-button" @click="checkVIP">只需￥114514成为尊贵的VIP用户</button>
    </div>

    <!-- VIP弹窗 -->
    <div v-if="isVIPModalVisible" class="modal">
      <div class="modal-content">
        <span class="modal-text">你已经是尊贵的VIP用户</span>
        <button class="modal-button" @click="closeVIPModal">我知道了</button>
      </div>
    </div>

    <!-- 充值弹窗 -->
    <div v-if="isRechargeModalVisible" class="modal">
      <div class="modal-content">
        <span class="modal-text">是否充值</span>
        <div class="modal-button-group">
          <button class="modal-button confirm-button" @click="confirmRecharge">确定</button>
          <button class="modal-button cancel-button" @click="cancelRecharge">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import store from '@/store/index.js' //需要引入store
  export default {
    data() {
      return {
        id: '', //保存用户ID
        vip: 0, //默认是0不是VIP
        position: 'right',
        animatedText1: '',
        animatedText2: '',
        animatedText3: '',
        isUnderlineExpanded: false,
        isPinkUnderlineExpanded: false,
        isVIPModalVisible: false,
        isRechargeModalVisible: false,
      }
    },
    //钩子函数
    mounted() {
      this.animateText1()
      this.animateText2()
      this.animateText3()
    },
    methods: {
      //下面这几条都是便于修改动态文字的方法
      animateText1() {
        const text = '高贵标志'
        let index = 0
        const interval = setInterval(() => {
          this.animatedText1 += text[index]
          index++
          if (index === text.length) {
            clearInterval(interval)
          }
        }, 100)
      },
      animateText2() {
        const text = '高额折扣'
        let index = 0
        const interval = setInterval(() => {
          this.animatedText2 += text[index]
          index++
          if (index === text.length) {
            clearInterval(interval)
          }
        }, 100)
      },
      animateText3() {
        const text = '行动起来'
        let index = 0
        const interval = setInterval(() => {
          this.animatedText3 += text[index]
          index++
          if (index === text.length) {
            clearInterval(interval)
          }
        }, 100)
      },

      //从数据库获取VIP信息判定弹窗内容
      checkVIP() {
        // 假设从后台获取到的 VIP 状态为 0 或 1
        const vipStatus = this.vip

        if (vipStatus == 1) {
          // 如果是 VIP 用户，则显示 VIP 弹窗
          this.isVIPModalVisible = true
        } else {
          // 如果不是 VIP 用户，则显示充值弹窗
          this.isRechargeModalVisible = true
        }
      },
      closeVIPModal() {
        // 关闭 VIP 弹窗
        this.isVIPModalVisible = false
      },
      async confirmRecharge() {
        const { data: res } = await uni.request({
          url: 'http://127.0.0.1:3007/api/beComeVIP',
          method: 'post',
          data: {
            id: this.id,
            vip: 1
          },
          header: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }

        })
        if (res.status === 0) {
          // 点击确定充值按钮
          // 执行充值操作
          // 成功后将用户设为 VIP
          this.isRechargeModalVisible = false
          this.isVIPModalVisible = true
        } else {
          uni.showToast({
            title: '申请失败',
            duration: 2000
          })
        }

      },
      cancelRecharge() {
        // 点击取消充值按钮
        // 不进行充值操作
        // 返回到 VIP 界面
        this.isRechargeModalVisible = false
        this.isVIPModalVisible = false // 将 isVIPModalVisible 设为 false，隐藏 VIP 弹窗
      }
    },
    onLoad() {
      this.id = store.state.user.User_ID
      if (store.state.user.User_VIP !== 0) {
        this.vip = store.state.user.User_VIP
      }

    }
  }
</script>

<style scoped>
  .main {
    background-color: rgba(239, 236, 235);
  }

  .container {
    display: flex;
    align-items: center;
    position: relative;
    background-color: rgba(239, 236, 235);
    margin-bottom: 30px;

  }

  .rectangle {
    width: 20px;
    height: 90px;
    background-color: skyblue;
    margin-right: 20px;
  }

  .content {
    display: flex;
    align-items: center;
    background-color: rgba(239, 236, 235);
  }

  .text {
    display: flex;
    flex-direction: column;
    margin-right: 10px;
  }

  .bold {
    font-weight: bold;
    font-size: 18px;
    overflow: hidden;
    white-space: nowrap;
  }

  .normal-text {
    font-size: 12px;
    margin-top: 10px;
    margin-left: 0px;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  }

  .animated-text {
    animation: fadeIn 1.5s ease-in-out forwards;
  }

  @keyframes fadeIn {
    0% {
      opacity: 0;
      transform: translateY(30px);
    }

    30% {
      opacity: 0.2;
      transform: translateY(-5px);
    }

    100% {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .image {
    margin-right: auto;
    margin-left: 0;
  }

  img {
    max-width: 100px;
    max-height: 100px;
    margin-right: 20px;
  }

  .underline {
    width: 94%;
    height: 2px;
    background-color: gold;
    position: absolute;
    bottom: -5px;
    right: 0;
  }


  /* 第二个容器的样式设计 */
  .pink-container {
    margin-bottom: 30px;
  }

  .pink-rectangle {
    width: 20px;
    height: 90px;
    background-color: pink;
    margin-right: 0;
    margin-left: auto;
  }

  .pink-underline {
    width: 94%;
    height: 2px;
    background-color: gold;
    position: absolute;
    bottom: -5px;
    left: 0;
    transition: width 5s ease-in-out;
  }

  /* 新增的类名样式 */
  .left-image {
    margin-right: 20px;
    margin-left: 10px;
  }

  /* VIP的样式设计 */
  .button-container {
    display: flex;
    justify-content: center;
    margin-top: 50px;
    /* 调整按钮与上面的距离的*/
  }

  .green-button {
    width: 90%;
    height: 40px;
    background-color: green;
    color: white;
    font-family: SimSun, '宋体', sans-serif;
    font-size: 16px;
    border-radius: 20px;
  }

  .modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(0, 0, 0, 0.5);
  }

  .modal-content {
    width: 300px;
    padding: 20px;
    background-color: white;
    border-radius: 10px;
    text-align: center;
  }

  .modal-text {
    font-family: SimSun, '宋体', sans-serif;
    font-size: 18px;
    color: black;
    margin-bottom: 20px;
  }

  .modal-button {
    width: 100px;
    height: 40px;
    background-color: green;
    color: white;
    font-family: SimSun, '宋体', sans-serif;
    font-size: 16px;
    border-radius: 20px;
    cursor: pointer;
    margin-top: 20px;
  }

  .modal-button-group {
    display: flex;
    justify-content: center;
    margin-top: 10px;
  }

  .modal-button+.modal-button {
    margin-left: 10px;
  }

  .modal-button.confirm-button {
    background-color: green;
  }

  .modal-button.cancel-button {
    background-color: red;
  }
</style>